package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.Md5Util;
import com.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    public UserMapper userMapper;
    // 查询用户
    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    // 注册
    @Override
    public void register(String username, String password) {
        // 1.加密
        String md5String = Md5Util.getMD5String(password);
        // 2.存储
        userMapper.add(username, md5String);

    }

    // 修改用户信息
    @Override
    public void updateUser(User user) {
        // 更新修改时间
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    // 更新用户头像
    @Override
    public void updateAvatar(String avatarUrl, Integer id) {
        userMapper.updateAvatra(avatarUrl, id);
    }

    // 更新用户密码
    @Override
    public void updatepwd(String new_pwd) {
        // 获取用户id
        Map<String ,Object> cliams = ThreadLocalUtil.get();
        Integer id = (Integer) cliams.get("id");
        String md5String = Md5Util.getMD5String(new_pwd);
        userMapper.updatePwd(md5String, id);
    }

}
